<?php

namespace app\admin\controller\statics;

use Throwable;
use ba\Random;
use app\common\controller\Backend;
use app\admin\model\Order as dbModel;
use app\admin\model\Goods as goods;
use app\admin\model\User as user;
use app\admin\model\Shop;
class Rank extends Backend
{
    /**
     * @var object
     * @phpstan-var UserModel
     */
    protected object $model;

    

    public function initialize(): void
    {
        parent::initialize();
        $this->model = new dbModel();
    }

    /**
     * 查看
     * @throws Throwable
     */
    public function index(): void
    {
        $rank_goods_sellnum = goods::where("status",1)->where("shop_id",$this->store_id)->order("sell_num desc")->field("title,feng,sell_num")->limit(10)->select();
        $rank_user_ordermoney = dbModel::alias('o')
                            ->where("o.is_pay",1)
                            ->where("o.shop_id",$this->store_id)
                            ->join("user u","o.user_id=u.id","LEFT")
                            ->field('o.user_id, SUM(o.money) as total_money,U.nickname')
                            ->group('o.user_id')
                            ->order('total_money', 'desc')
                            ->limit(10)
                            ->select();
        $this->success('', [
            'list'   => [
                'rank_goods_sellnum'=>$rank_goods_sellnum,
                'rank_user_ordermoney'=>$rank_user_ordermoney
            ]
        ]);
    }


    function dash(){
        $n['newuser'] = user::where(['shop_id'=>$this->store_id])
        ->whereTime('create_time', '>=', date('Y-m-d', time()))
        ->whereTime('create_time', '<', date('Y-m-d', time() + 86400))
        ->count();

        $n['today_order'] = dbModel::where(['shop_id'=>$this->store_id,'date'=>date("Y-m-d"),'is_pay'=>1])->sum("money");

        $n['sum_order'] = dbModel::where(['shop_id'=>$this->store_id,'is_pay'=>1])->sum("money");

        $n['tui_order'] = dbModel::where(['shop_id'=>$this->store_id,'is_pay'=>1,'order_status'=>7])->count();

        $store = Shop::where('sid',$this->store_id)->find()->toArray();
        

        $charts = [];
        $today_order_os = dbModel::where(['shop_id'=>$this->store_id,'date'=>date("Y-m-d"),'is_pay'=>1])->field("order_status")->select();
        $d = date("Y-m-d");
        for($i=0;$i<7;$i++){
              
              $date=date("Y-m-d", strtotime("-".$i." day $d"));
              $date_9[] = $date;

              $map = [];
              $map[] = ['create_time','>=',strtotime($date." 00:00:00")];
              $map[] = ['create_time','<=',strtotime($date." 23:59:59")];
              $user_chart[] = user::where(['shop_id'=>$this->store_id])->where($map)->count();
        }
        $charts['date_9'] = $date_9;
        $charts['user_chart'] = $user_chart;

        //获取订单状态统计
        $os_arr = [
            ['s'=>'daifu','name'=>'待付款'],
            ['s'=>'daifa','name'=>'待发货'],
            ['s'=>'daishou','name'=>'待收货'],
            ['s'=>'tui','name'=>'申请退款'],
            ['s'=>'tuied','name'=>'已退款'],
            ['s'=>'complete','name'=>'交易完成'],
        ];
        $os_count = [];
        foreach($os_arr as &$v){
             $w = [];
             $w[] = ['shop_id','=',$this->store_id];
             $w2 = order_status_map($v['s']);
             if(!empty(($w2))){
                $w = array_merge($w,$w2);
             }
             $count = dbModel::where($w)->count();
             $os_count[] = ['name'=>$v['name'],'count'=>$count];
        }
        $charts['os_chart'] = $os_count;

        //获取订单统计
        for($i=0;$i<15;$i++){
              
              $date=date("Y-m-d", strtotime("-".$i." day $d"));
              $date_15[] = $date;

              $order_chart[] = dbModel::where(['shop_id'=>$this->store_id,'is_pay'=>1,'date'=>$date])->sum("money");
        }
        $charts['date_15'] = $date_15;
        $charts['order_chart'] = $order_chart;

        $this->success("ok",['sumdata'=>$n,'store'=>$store,'charts'=>$charts]);
    }

}